package com.tjrac.zhaopin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.tjrac.zhaopin.entity.EnterpriseInfo;
import com.tjrac.zhaopin.entity.SysUser;
import com.tjrac.zhaopin.mapper.EnterpriseInfoMapper;
import com.tjrac.zhaopin.service.EnterpriseInfoService;
import com.tjrac.zhaopin.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/admin")
public class AdminController {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    EnterpriseInfoService enterpriseInfoService;
    @Autowired
    EnterpriseInfoMapper enterpriseInfoMapper;

    /**
     * 列出企业信息
     *
     * @return
     */
    @GetMapping("/listAllEnterprise")
    public R listAllEnterprise() {
        try {
            return R.ok(enterpriseInfoMapper.listEnterpriseInfoWithUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("失败");
        }
    }

    /**
     * 通过审核企业信息
     */
    @GetMapping("/auditEnterprise")
    @Transactional(rollbackFor = Exception.class)
    public R auditEnterprise(Integer userId) {
        try {
            EnterpriseInfo info = enterpriseInfoService.getOne(new QueryWrapper<EnterpriseInfo>()
                    .eq("holder_id", userId));
            if (info != null) {
                info.setVerified(1);
                enterpriseInfoService.updateById(info);
                return R.ok("成功");
            } else {
                return R.failed("失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("失败");
        }
    }

    /**
     * 删除企业信息
     */
    @GetMapping("/deleteEnterprise")
    @Transactional(rollbackFor = Exception.class)
    public R deleteEnterprise(Integer userId){
        try {
            EnterpriseInfo info = enterpriseInfoService.getOne(new QueryWrapper<EnterpriseInfo>()
                    .eq("holder_id", userId));
            if (info != null) {
                enterpriseInfoService.removeById(info.getId());
                return R.ok("成功");
            } else {
                return R.failed("失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.failed("失败");
        }
    }

    /**
     * 列出用户信息
     *
     * @return
     */
    @GetMapping("/listAllUsers")
    public R listAllUsers() {
        try {
            return R.ok(sysUserService.list());
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("失败");
        }
    }


    /**
     * 删除用户信息
     */
    @GetMapping("/deleteUser")
    @Transactional(rollbackFor = Exception.class)
    public R deleteUser(Integer userId){
        try {
            SysUser info = sysUserService.getOne(new QueryWrapper<SysUser>()
                    .eq("id", userId));
            if (info != null) {
                sysUserService.removeById(info.getId());
                return R.ok("成功");
            } else {
                return R.failed("失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.failed("失败");
        }
    }


}
